<template>
  <div class="app-container">
    <!--    表单查询区-->
    <div class="filter-container">
      <div class="filter-item" style="margin-right: 5px;">课程名：</div>
      <el-select v-model="listQuery.id" class="filter-item" filterable placeholder="请选择" style="margin-right: 10px;" @change="getEvaluatePage">
        <el-option
          v-for="item in courseList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        导出
      </el-button>
    </div>

    <!--表格区-->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" type="index" width="60px" align="center" />
      <el-table-column label="用户名" prop="username" align="center" />
      <el-table-column label="评价得分" align="center">
        <template v-slot="{row}">
          <el-rate
            v-model="row.score"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          />
        </template>
      </el-table-column>
      <el-table-column label="评价内容" prop="text" align="center" />
      <el-table-column label="评价时间" align="center">
        <template v-slot="{row}">
          {{ parseTime(row.updateTime, '{y}年{m}月{d}日 {h}:{m}:{s}') }}
        </template>
      </el-table-column>
    </el-table>

    <!--分页插件-->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getEvaluatePage" />

  </div>
</template>

<script>

import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { getCourseListByRole } from '@/api/course'
import { getEvaluateByCourseId, getEvaluatePage } from '@/api/evaluate'
import { parseTime } from '@/utils'

export default {
  name: 'Index',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: [],
      listQuery: {
        key: '',
        current: 1,
        size: 10,
        // 选择器绑定课程id
        id: undefined
      },
      listLoading: false,
      courseList: [],
      // 评价总条数
      total: 0,
      downloadLoading: false
    }
  },
  created() {
    this.getCourseList()
  },
  methods: {
    parseTime,
    // 根据用户角色获取课程列表
    getCourseList() {
      getCourseListByRole().then(res => {
        this.courseList = res.data
        if (this.courseList <= 0) {
          this.courseList.unshift({
            id: 0,
            name: '暂无课程'
          })
          this.listQuery.id = 0
        } else {
          this.listQuery.id = res.data[0].id
          this.getEvaluatePage()
        }
      })
    },
    // 根据课程id获取课程评价列表
    getEvaluatePage() {
      getEvaluatePage(this.listQuery).then(res => {
        this.list = res.data.records
        this.total = res.data.total
      }).catch(() => {
        this.list = []
        this.total = 0
      })
    },
    // 导出函数
    handleDownload() {
      this.downloadLoading = true
      let courseName = ''
      this.courseList.forEach(course => {
        if (course.id === this.listQuery.id) {
          courseName = course.name
        }
      })
      getEvaluateByCourseId(this.listQuery.id).then(res => {
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['序号', '评分', '内容', '创建时间', '更新时间']
          const filterVal = ['id', 'score', 'text', 'createTime', 'updateTime']
          const data = this.formatJson(filterVal, res.data)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: courseName + '_评价列表' + parseTime(new Date(), '{y}-{m}-{d}-{h}-{i}-{s}')
          })
          this.downloadLoading = false
        })
      }).catch(() => {
        this.downloadLoading = false
      })
    },
    // 格式化下载数据
    formatJson(filterVal, list) {
      return list.map(v => filterVal.map(j => {
        if (j === 'createTime' || j === 'updateTime') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>

<style scoped>

</style>
